package com.cpi.cornpp.purchase.program.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.purchase.program.dto.PurchaseProgramDTO;
import com.cpi.cornpp.purchase.program.view.PurchaseProgramView;
import com.cs.common.init.SystemParameterCollection;
import com.cs.common.init.beans.SystemParameterBean;
import com.cs.common.utils.NumberUtil;

/**
 * Purchase Program Converter
 * 
 * @author Edgar Joao
 *
 */
public class PurchaseProgramConverter {

	private static final String PADDING_PARAM = "PADDING_FOR_CODE";


	/**
	 * Purchase Program Converter View to DTO
	 * @param view
	 * @return
	 */
	public PurchaseProgramDTO converterPurchaseProgramViewToDTO(PurchaseProgramView view){
		PurchaseProgramDTO purchaseProgramDTO = null;
		purchaseProgramDTO = new PurchaseProgramDTO();
		purchaseProgramDTO.setPeriod(view.getPeriod());
		purchaseProgramDTO.setbNo(view.getbNo());
		purchaseProgramDTO.setbNoNumber(NumberUtil.parseInt(view.getbNo()));
		purchaseProgramDTO.setFutures(view.getFutures());
		purchaseProgramDTO.setTons(view.getTons());
		purchaseProgramDTO.setPriceBu(view.getPriceBu());
		purchaseProgramDTO.setBaseBu(view.getBaseBu());
		purchaseProgramDTO.setPriceUsBu(view.getPriceUsBu());
		purchaseProgramDTO.setPriceUsTm(view.getPriceUsTm());
		purchaseProgramDTO.setValRealFut(view.getValRealFut());
		purchaseProgramDTO.setValBase(view.getValBase());
		purchaseProgramDTO.setcRealTm(view.getcRealTm());
		purchaseProgramDTO.setPurchaseOrderId(view.getPurchaseOrderId());
		purchaseProgramDTO.setBiweekPeriodInitId(view.getBiweekPeriodInitId());
		purchaseProgramDTO.setBiweekPeriodFinishId(view.getBiweekPeriodFinishId());
		return purchaseProgramDTO;		
	}
	
	
	/**
	 * Purchase Program Converter DTO to View
	 * @param dto
	 * @return
	 */
	public PurchaseProgramView converterPurchaseProgramDtoToView(PurchaseProgramDTO dto){
		SystemParameterBean padding = (SystemParameterBean)SystemParameterCollection.getInstance().getSystemParameterBean(PADDING_PARAM);
		PurchaseProgramView purchaseProgramView = null;
		purchaseProgramView = new PurchaseProgramView();
		purchaseProgramView.setPeriod(dto.getPeriod());
		purchaseProgramView.setbNo(dto.getbNo()!=null?dto.getbNo().trim():dto.getbNo());
		purchaseProgramView.setBnoNumber(StringUtils.leftPad( String.valueOf(dto.getbNoNumber()), padding.getIntValue(),"0"));
		purchaseProgramView.setFutures(dto.getFutures());
		purchaseProgramView.setTons(dto.getTons());
		purchaseProgramView.setPriceBu(dto.getPriceBu());
		purchaseProgramView.setBaseBu(dto.getBaseBu());
		purchaseProgramView.setPriceUsBu(dto.getPriceUsBu());
		purchaseProgramView.setPriceUsTm(dto.getPriceUsTm());
		purchaseProgramView.setValRealFut(dto.getValRealFut());
		purchaseProgramView.setValBase(dto.getValBase());
		purchaseProgramView.setcRealTm(dto.getcRealTm());
		purchaseProgramView.setPurchaseOrderId(dto.getPurchaseOrderId());
		purchaseProgramView.setBiweekPeriodInitId(dto.getBiweekPeriodInitId());
		purchaseProgramView.setBiweekPeriodFinishId(dto.getBiweekPeriodFinishId());
		purchaseProgramView.setSapAssigned(dto.getSapAssigned());
		purchaseProgramView.setFutureAssigned(dto.getFutureAssigned());
		purchaseProgramView.setBushelPriceAssigned(dto.getBushelPriceAssigned());
		return purchaseProgramView;
	}
	
	
	/**
	 * Purchase Program Converter List View to DTO 
	 * @param plantDTOs
	 * @return
	 */
	public List converterViews(List purchaseProgramDTOs){
		List purchaseProgramsDTO = null;
		purchaseProgramsDTO = new ArrayList(0);
		
		if(!CollectionUtils.isEmpty(purchaseProgramDTOs)){
			Iterator iterator = purchaseProgramDTOs.iterator();
			PurchaseProgramView view = null;
			while(iterator.hasNext()){
				view = (PurchaseProgramView) iterator.next();
				purchaseProgramsDTO.add(view);
			}
		}
		return purchaseProgramsDTO;
	}
		
	
	/**
	 * Purchase Program Converter List DTO to View
	 * @param plantDTOs
	 * @return
	 */
	public List converterDTOs(List purchaseProgramViews){
		List purchaseProgramsView = null;
		purchaseProgramsView = new ArrayList(0);
		
		if(!CollectionUtils.isEmpty(purchaseProgramViews)){
			Iterator iterator = purchaseProgramViews.iterator();
			PurchaseProgramDTO dto = null;
			while(iterator.hasNext()){
				dto = (PurchaseProgramDTO) iterator.next();			
				purchaseProgramsView.add(this.converterPurchaseProgramDtoToView(dto));
			}
		}
		return purchaseProgramsView;
	}
	
}
